home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWThread / FWThrdGd.h < prev    next >
Encoding:
Text File  |  1996-09-17  |  4.9 KB  |  146 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWThrdGd.h
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef FWTHRDGD_H
  11. #define FWTHRDGD_H
  12.  
  13. #ifndef FWSTDDEF_H
  14. #include "FWStdDef.h"
  15. #endif
  16.  
  17. #ifdef FW_BUILD_MAC
  18. #include <Threads.h>
  19. #endif
  20.  
  21. //==============================================================================
  22. // Theory of Operation
  23. //==============================================================================
  24.  
  25. // FW_CThreadSafe is a class exporting static methods which parts 
  26. // must call if they are using threads.  The methods should be called 
  27. // within a critical region; they are not thread-safe.
  28.  
  29. // FW_CThreadGuard is an abstract base class associating threads with operations 
  30. // which must be done to assure thread-safe execution.  Instances of FW_CThreadGuard 
  31. // are always statically allocated and provide protection for global variables.
  32.  
  33. //==============================================================================
  34. // Constants
  35. //==============================================================================
  36.  
  37. //==============================================================================
  38. // Scalar Types
  39. //==============================================================================
  40.  
  41. #if defined    FW_BUILD_MAC
  42. typedef    ThreadID            FW_ThreadID;
  43. #elif defined FW_BUILD_WIN
  44. typedef    unsigned long        FW_ThreadID;
  45. #endif
  46.  
  47. //=====================================================================================
  48. // Classes defined in this interface
  49. //=====================================================================================
  50.  
  51. class FW_CThreadSafe;    
  52. class FW_CThreadGuard;    
  53.  
  54. //=====================================================================================
  55. // Classes used by this interface
  56. //=====================================================================================
  57.  
  58. //=====================================================================================
  59. // Class FW_CThreadSafe
  60. //=====================================================================================
  61.  
  62. class FW_CThreadSafe {
  63. public:
  64.     static void NoteCreation(FW_ThreadID newlyCreatedThread);
  65.     static void NoteTermination(FW_ThreadID threadBeingKilled);
  66.         // Parts call NoteCreation when creating a new thread and 
  67.         // NoteTermination when a thread is being destroyed.
  68.  
  69. #ifdef    FW_BUILD_MAC
  70.     static void NoteSwitch(FW_ThreadID aThread, FW_Boolean switchingIn);
  71.         // A thread-using part must call this method when a thread 
  72.         // switch occurs.  The boolean is TRUE when the thread is being
  73.         // switched in, FALSE when the thread is being switched out.
  74. #endif
  75.  
  76. private:
  77.     friend class FW_CThreadGuard;
  78.     static void RegisterTerminationGuard(FW_CThreadGuard *aThreadGuard);
  79.     static void DeRegisterTerminationGuard(FW_CThreadGuard *aThreadGuard);
  80.  
  81.     static void RegisterSwitchGuard(FW_CThreadGuard *aThreadGuard);
  82.     static void DeRegisterSwitchGuard(FW_CThreadGuard *aThreadGuard);
  83.  
  84. private:
  85.     static FW_CThreadGuard *fgSwitchGuards;
  86.     static FW_CThreadGuard *fgTerminationGuards;
  87. };
  88.  
  89.  
  90. //=====================================================================================
  91. // Class FW_CThreadGuard
  92. //=====================================================================================
  93.  
  94. class FW_CThreadGuard {
  95. protected:
  96.     enum {
  97.         kNoThreadEvent            = 0x00,
  98.         kSwitchInThreadEvent    = 0x01,
  99.         kSwitchOutThreadEvent   = 0x02,
  100.  
  101.         kAllThreadEvents = kSwitchInThreadEvent | kSwitchOutThreadEvent
  102.     };
  103.  
  104.     FW_CThreadGuard(unsigned long threadEventMask = kAllThreadEvents);
  105.     virtual ~FW_CThreadGuard();
  106.  
  107.     virtual void Created(FW_ThreadID newlyCreatedThread) = 0;
  108.     virtual void Terminating(FW_ThreadID threadBeingKilled) = 0;
  109.         // These methods are called when threads are created and
  110.         // terminated.
  111.  
  112. #ifdef    FW_BUILD_MAC
  113.     virtual void Switch(FW_ThreadID aThread, FW_Boolean switchingIn);
  114.         // Called when a thread switch takes place.  This is an optional
  115.         // notification: if the constructor did not have kSwitchThreadEvent
  116.         // in its bitmask the guard will not be notified of thread switches.
  117.         // switchingIn is TRUE if aThread will be next to run, FALSE if it 
  118.         // being switched out.
  119. #endif
  120.  
  121.     void AddThreadInfo(FW_ThreadID aThreadID, void *threadInfo);
  122.     void RemoveThreadInfo(FW_ThreadID aThreadID);
  123.     void *GetThreadInfo(FW_ThreadID aThreadID);
  124.         // These methods allow instances of derived classes to associate 
  125.         // a chunk of data with a thread.  AddThreadInfo() should be called 
  126.         // from "Created()", RemoveThreadInfo() should be called from 
  127.         // "Terminating()" and GetThreadInfo() from "Switching()".
  128.  
  129.     unsigned long GetMask() const;
  130.  
  131. private:
  132.     friend class FW_CThreadSafe;
  133.     FW_CThreadGuard *fNext;
  134.     unsigned long fMask;
  135.  
  136. private:
  137.     struct FW_SThreadItem {
  138.         FW_ThreadID     fThreadID;
  139.         void           *fThreadInfo;
  140.         FW_SThreadItem *fNext;
  141.     };
  142.     FW_SThreadItem *fActiveThreads;
  143. };
  144.  
  145. #endif
  146.